﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CoVanHocTapData.Infrastructure;
using CoVanHocTapData.Entities;

namespace CoVanHocTapData.Concretes
{
    public class KhoiChuongTrinhDaoTaoRepository : IKhoiChuongTrinhDaoTaoRepository
    {
        DangKyPhuEntities entites;
        public KhoiChuongTrinhDaoTaoRepository()
        {
            entites = new DangKyPhuEntities();
        }

        /// <summary>
        /// lay cac khoi tu chon va khoi cua nhom luan van theo ctdt chuan
        /// </summary>
        /// <param name="nganhID"></param>
        /// <param name="nienKhoaID"></param>
        /// <param name="heDaoTaoID"></param>
        /// <returns></returns>
        public IQueryable<Entities.KhoiChuongTrinhDaoTao> LayKhoiCTDT(string nganhID, int nienKhoaID, string heDaoTaoID)
        {

            var result = (from m in entites.KhoiChuongTrinhDaoTaos
                          where m.NganhID.Equals(nganhID)
                          && m.NienKhoaID.Value == nienKhoaID
                          && m.HeDaoTaoID.Equals(heDaoTaoID)
                          && (m.LoaiKhoi==0 || m.LoaiKhoi==1)
                          select m);

            return result;

        }

        /// <summary>
        /// lay khoi ctdt theo hoc ky, doChinhXac la true thi lay dug hoc ky, false thi lay gan dug dua vao hocky%2
        /// </summary>
        /// <param name="nganhID"></param>
        /// <param name="nienKhoaID"></param>
        /// <param name="heDaoTaoID"></param>
        /// <param name="hocKyID"></param>
        /// <param name="doChinhXac"></param>
        /// <returns></returns>
        public List<Entities.KhoiChuongTrinhDaoTao> LayKhoiCTDT(string nganhID, int nienKhoaID, string heDaoTaoID, int hocKyID, bool doChinhXac)
        {
            IQueryable<KhoiChuongTrinhDaoTao> khoictdt ;
            List<KhoiChuongTrinhDaoTao> result = new List<KhoiChuongTrinhDaoTao>();
            int i = 0;
            while (true)
            {
                khoictdt = LayKhoiCTDT(nganhID, nienKhoaID - i, heDaoTaoID);
                if (khoictdt != null && khoictdt.Count()>0)
                    break;
                i++;
            }
            string hocKyString = hocKyID.ToString();

            foreach (var item in khoictdt)
            {
                string[] hocKySplit = item.HocKys.Split(',');
                if (!doChinhXac)
                {
                    foreach (var hocKySplitItem in hocKySplit)
                    {
                        if (hocKySplitItem != "" && int.Parse(hocKySplitItem) % 2 == 0)
                        {
                            if (hocKyID % 2 == 0)
                                result.Add(item);
                        }
                        else if (hocKySplitItem != "" && int.Parse(hocKySplitItem) % 2 != 0)
                        {
                            if (hocKyID % 2 != 0)
                                result.Add(item);
                        }
                    }
                }
                else
                {
                    if (hocKySplit.Contains(hocKyString))
                    {
                        result.Add(item);
                    }
                }
            }
            return result;
            
            

            //return (from m in entites.KhoiChuongTrinhDaoTaos
            //           where m.NganhID.Equals(nganhID)
            //           && m.NienKhoaID.Value == nienKhoaID
            //           && m.HeDaoTaoID.Equals(heDaoTaoID)
            //           && m.HocKys.Contains(hocKyString)
            //           select m);
        }


        public string LayHocKys(string nganhID, int nienKhoaID, string heDaoTaoID)
        {
            return (from m in entites.KhoiChuongTrinhDaoTaos
                    where m.NganhID.Equals(nganhID)
                    && m.NienKhoaID.Value == nienKhoaID
                    && m.HeDaoTaoID.Equals(heDaoTaoID)
                    select m.HocKys).FirstOrDefault();
        }

        public KhoiChuongTrinhDaoTao LayKhoiCTDTTuMaMonHoc(string monHocID, string nganhID)
        {
            return (from m in entites.KhoiChuongTrinhDaoTaos
                    join n in entites.ChiTietKhoiCTDTs
                    on m.ID equals n.KhoiChuongTrinhDaoTaoID
                    where n.MonHocID == monHocID
                    && m.NganhID == nganhID
                    select m).FirstOrDefault();
        }

        public KhoiChuongTrinhDaoTao LayKhoiCTDTTuMaSV(string maSV)
        {
            KhoiLopRepository khoiLopRepo = new KhoiLopRepository();
            NganhDaoTao nganhDaoTao = new NganhDaoTao();
            NienKhoa nienKhoa = new NienKhoa();
            HeDaoTao heDaoTao = new HeDaoTao();

            khoiLopRepo.KhoiLop(maSV, out nganhDaoTao, out heDaoTao, out nienKhoa);

            var khoiCTDT = (from m in entites.KhoiChuongTrinhDaoTaos
                              where m.HeDaoTaoID == heDaoTao.HeDaoTaoID
                              && m.NganhID == nganhDaoTao.NganhID
                              && m.NienKhoaID == nienKhoa.NienKhoaID
                              select m).FirstOrDefault();
            return khoiCTDT;
        }

        public List<KhoiChuongTrinhDaoTao> DSKhoiNhomCTDT(string maSV)
        {
            KhoiLopRepository khoiLopRepo = new KhoiLopRepository();
            NganhDaoTao nganhDaoTao = new NganhDaoTao();
            NienKhoa nienKhoa = new NienKhoa();
            HeDaoTao heDaoTao = new HeDaoTao();

            khoiLopRepo.KhoiLop(maSV, out nganhDaoTao, out heDaoTao, out nienKhoa);

            var dsKhoiNhom = (from m in entites.KhoiChuongTrinhDaoTaos
                              where m.HeDaoTaoID == heDaoTao.HeDaoTaoID
                              && m.NganhID == nganhDaoTao.NganhID
                              && m.NienKhoaID == nienKhoa.NienKhoaID
                              select m).ToList();
            return dsKhoiNhom;
        }

        public List<KhoiChuongTrinhDaoTao> DSKhoiNhomCTDT_CN2(string maSV)
        {
            KhoiLopRepository khoiLopRepo = new KhoiLopRepository();
            NganhDaoTao nganhDaoTao = new NganhDaoTao();
            NienKhoa nienKhoa = new NienKhoa();
            HeDaoTao heDaoTao = new HeDaoTao();

            khoiLopRepo.KhoiLopCN2(maSV, out nganhDaoTao, out heDaoTao, out nienKhoa);

            var dsKhoiNhom = (from m in entites.KhoiChuongTrinhDaoTaos
                              where m.HeDaoTaoID == heDaoTao.HeDaoTaoID
                              && m.NganhID == nganhDaoTao.NganhID
                              && m.NienKhoaID == nienKhoa.NienKhoaID
                              select m).ToList();
            return dsKhoiNhom;
        }

        
    }
}
